// 图片导出
function canvasToTempFilePath({
  x,
  y,
  width,
  height,
  destWidth,
  destHeight,
  canvasId,
  fileType = 'jpg',
  quality = 1
} = {}, pointer) {
  let pomise = new Promise((resolve, reject) => {
    wx.canvasToTempFilePath({
      x: x,
      y: y,
      width: width,
      height: height,
      destWidth: destWidth,
      destHeight: destHeight,
      canvasId: canvasId,
      fileType: fileType,
      quality: quality,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    }, pointer);
  });
  return pomise;
}

// 获取用户的当前设置
function getSetting({
  withSubscriptions = false
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.getSetting({
      withSubscriptions: withSubscriptions,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 提前向用户发起授权请求
function authorize({
  scope
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.authorize({
      scope: scope,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 拨打电话
function makePhoneCall({
  phoneNumber
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 显示消息提示框
function showToast({
  title,
  icon = "none",
  image,
  duration = 1500,
  mask = true
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.showToast({
      title: title,
      icon: icon,
      image: image,
      duration: duration,
      mask: mask,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}

// 打开另一个小程序
function navigateToMiniProgram({
  appId,
  path,
  extraData,
  envVersion,
} = {}) {
  let pomise = new Promise((resolve, reject) => {
    wx.navigateToMiniProgram({
      appId: appId,
      path: path,
      extraData: extraData,
      envVersion: envVersion,
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
  return pomise;
}



module.exports = {
  canvasToTempFilePath,
  getSetting,
  authorize,
  makePhoneCall,
  showToast,
  navigateToMiniProgram
}
